package map;

import java.util.concurrent.Callable;

import settings.GraphicSettings;

import com.jme.image.Texture;
import com.jme.scene.Skybox;
import com.jme.util.GameTaskQueueManager;

import fileHandling.TextureLoader;

public class SpaceBackground extends Skybox {
	
	private static final long serialVersionUID = 1L;
	
	private String currentTexQuality;
	
	private String skyboxPath;
	
	public SpaceBackground(String skyboxName, int dimension) {
		super("Skybox", dimension * 10, dimension * 10, dimension * 10);
		
		skyboxPath = "data/textures/skyboxes/" + skyboxName + "/";
		updateTextureQuality();
		
		lock();
	}
	
	public void updateTextureFilters() {
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.North));
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.West));
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.South));
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.East));
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.Up));
		TextureLoader.upateTextureFilter(getTexture(Skybox.Face.Down));
	}
	
	public boolean updateTextureQuality() {
		if(currentTexQuality != null && 
		currentTexQuality.equals(GraphicSettings.get().getTextureQuality())) return false;
		currentTexQuality = GraphicSettings.get().getTextureQuality();
				
		String texQualPath = skyboxPath + currentTexQuality + "/";
		Texture north = TextureLoader.getTextureState(texQualPath + "back.png").getTexture();
		Texture south = TextureLoader.getTextureState(texQualPath + "front.png").getTexture();
		Texture east = TextureLoader.getTextureState(texQualPath + "right.png").getTexture();
		Texture west = TextureLoader.getTextureState(texQualPath + "left.png").getTexture();
		Texture up = TextureLoader.getTextureState(texQualPath + "up.png").getTexture();
		Texture down = TextureLoader.getTextureState(texQualPath + "down.png").getTexture();
		
		setTexture(Skybox.Face.North, north);
		setTexture(Skybox.Face.West, west);
		setTexture(Skybox.Face.South, south);
		setTexture(Skybox.Face.East, east);
		setTexture(Skybox.Face.Up, up);
		setTexture(Skybox.Face.Down, down);
		
		preloadTextures();
		
		return true;
	}
	
	public void updateGraphicSettings() {
		try {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				@Override
				public Void call() throws Exception {
					if(!updateTextureQuality()) updateTextureFilters();
					return null;
				}
			}).get();
		} catch(Exception e) {}
	}
}